package com.whq.mvc.base.juc;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class CountDownLatchDemo1 {

    public static void main(String[] args) throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(1);
        CountDownLatch latch1 = new CountDownLatch(5);

        for (int i = 0; i < 5; i++) {
            new MyThread(latch, latch1).start();
        }
        System.out.println("主线程执行--");
        TimeUnit.SECONDS.sleep(3);
        latch.countDown();
        latch1.await();
        System.out.println("主线程执行完了");
    }

    static class MyThread extends Thread {

        CountDownLatch latch;
        CountDownLatch latch1;

        public MyThread(CountDownLatch latch, CountDownLatch latch1) {
            this.latch = latch;
            this.latch1 = latch1;
        }

        @Override
        public void run() {
            try {
                latch.await();
                System.out.println("当前线程" + Thread.currentThread().getName() + "正在执行--");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } finally {
                latch1.countDown();
            }
        }
    }
}
